import {
  api,
  util,
  storage
} from "../util";
import authorize from './authorize.js'
const app = getApp();
const distribution = {
  //提现记录
  loadCashOutRecord(args, callback) {
    var entity = authorize.getEntity();
    var param = {
      Status: args.status,
      PageIndex: args.pageIndex || 1,
      PageSize: args.pageSize || 20,
      Token: entity.manager.Token,
      sortField: "Id",
      sortDirect: "desc",
      ProprietorId: app.dns.proprietorId
    };
    //1 分销提现记录  2 预存款提现记录
    util.post(args.types==1?"distribution.distributor.distributionwithdrawlist":"finance.withdraw.listpage", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //分销佣金
  loadCommisionList(userId,args, callback) {
    var entity = authorize.getEntity();
    var param = {
      ...args,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    var url='distribution.distributor.distributioncommission'
    if(userId>0){
      url = 'distribution.distributor.subdistributioncommission'
      param.DistributorId = userId
    }
    util.post(url, param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //分销中心基本合计数据接口
  dataStatistics(callback){
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.datastatistics", param,function(res){
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof(callback) == "function") {
          var content = res.Content[0];
          callback(content);
        }
      }else{
        util.tips(res.Message);
      }
    },"3.0",null,false)
  },
  //分销中心新增客户统计
  getAddCustomerStatistics(args,callback){
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      DateType: args.dateType,
      QueryDate: args.dateTime,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.addcustomersummary", param,function(res){
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof(callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    },"3.0",null,false)
  },
  //下级客户
  loadSubOrdinate(args, callback) {
    var entity = authorize.getEntity();
    var param = {
      Layer: args.status || 1,
      PageIndex: args.pageIndex || 1,
      PageSize: args.pageSize || 20,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.subdistributor", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //分销订单
  loadDistributeOrder(args, callback) {
    var entity = authorize.getEntity();
    var param = {
      Keyword: args.keyword || "",
      PageIndex: args.pageIndex || 1,
      PageSize: args.pageSize || 20,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.order", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //排名
  loadDistributeRank(type,args, callback) {
    var entity = authorize.getEntity();
    var param = {
      ...args,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    var url='distribution.distributor.rank'
    if(type!=1){
      url='distribution.distributor.commission'
    }
    util.post(url, param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //分销订单商品清单 
  loadGoodsList(args, callback) {
    var entity = authorize.getEntity();
    var param = {
      distributionorderId: args.id || "",
      PageIndex: args.pageIndex || 1,
      PageSize: args.pageSize || 20,
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.products", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    }, "3.0")
  },
  //申请分销条件
  getDistributionLimit(callback){
    var entity = authorize.getEntity();
    var param = {
      Token:entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("distribution.distributor.limit",param,function(res){
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    },"3.0",null,false)
  },
  //申请分销
  applyDistribution(args,callback){
    var entity = authorize.getEntity();
    let param = {
      retry:args.retry,
      Token:entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    }
    util.post("distribution.distributor.apply",param,function(res){
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          var content = res.Content;
          callback(content);
        }
      }
    },"3.0",null,false)
  },
    //提现页面数据
    withdrawableData(callback) {
      var entity = authorize.getEntity();
      var param = {
        Token: entity.manager.Token,
        ProprietorId: app.dns.proprietorId
      };
      util.post("distribution.distributor.datastatistics", param, function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            var content = res.Content;
            callback(content);
          }
        }
      }, "3.0")
    },
    //提现页面银行账户
    distributionList(callback) {
      var entity = authorize.getEntity();
      var param = {
        FKFlag: entity.fkFlag,
        FKId: entity.fkId,
        Token: entity.manager.Token,
        ProprietorId: app.dns.proprietorId
      };
      util.post("finance.cashaccount.distributionlist", param, function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            var content = res.Content;
            callback(content);
          }
        }
      }, "3.0")
    },
    //提现-分销
    cashOut(args,callback) {
      var entity = authorize.getEntity();
      var param = {
        ...args,
        Token: entity.manager.Token,
        ProprietorId: app.dns.proprietorId
      };
      util.post("distribution.distributor.distributionwithdrawadd", param, function (res) {
        res = res.data;
        if (res.Success) {
          util.success("提交成功")
          if (callback != undefined && typeof (callback) == "function") {
            callback();
          }
        }
          else{
            util.alert(res.Message)
          }
      }, "3.0")
    },
    //提现- 预存款
    withdrawAdd(args,callback){
      var entity = authorize.getEntity();
      var param = {
        ...args,
        Token: entity.manager.Token,
        ProprietorId: app.dns.proprietorId
      };
      util.post("finance.withdraw.add", param, function (res) {
        res = res.data;
        if (res.Success) {
          util.tips("提交申请成功，资金很快就会到账");
          if (callback != undefined && typeof (callback) == "function") {
            callback();
          }
        }
          else{
            util.alert(res.Message)
          }
      }, "3.0")
    },
}
export {
  distribution
}